package com.lanxin.hr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanxin.hr.entity.ZzAccount;
import com.lanxin.hr.service.IZzAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-26
 */
@Controller
@RequestMapping("/hr/zz-account")
public class ZzAccountController {

    @Autowired
    private IZzAccountService accountService;

    //检查账号是否已经存在
    @RequestMapping("/checkUsername")
    @ResponseBody
    public String checkUsername(@RequestParam(required = false) Integer aid,@RequestParam String username) {
        ZzAccount account = accountService.getAccountByUsername(username);
        boolean flag = false;
        if (account == null) {
            flag = true;
        }else if (account != null && account.getAid() == aid) {
            flag = true;
        }
        String json=null;
        Map<String,Object> map=new HashMap<>();
        map.put("valid",flag);
        ObjectMapper mapper = new ObjectMapper();
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    @RequestMapping("/toUpdatePassword")
    //跳转修改密码页面
    public String toUpdatePassword() {
        return "hr/account/updatePassword";
    }

    //修改密码
    @RequestMapping("/updatePassword")
    public String updatePassword(HttpSession session,@RequestParam String newPassword) {
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        account.setPassword(newPassword);
        int row = accountService.updatePassword(account);
        if (row > 0) {
            return "redirect:/logout";
        }
        return null;
    }


    //通过账号密码查询账号信息，用于修改密码时验证原密码是否正确
    @RequestMapping("/checkOldPassword")
    @ResponseBody
    public String checkOldPassword(HttpSession session, @RequestParam String oldPassword) {
        ZzAccount account = (ZzAccount) session.getAttribute("account");
        account.setPassword(oldPassword);
        ZzAccount account2 = accountService.getOne(new QueryWrapper<ZzAccount>().eq("username",account.getUsername()).eq("password",account.getPassword()));
        Map<String,Object> map=new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        String json=null;
        if(account2!=null){
            map.put("valid",true);
        }else {
            map.put("valid",false);
        }
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

}
